﻿using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

namespace BasicTools
{
    /// <summary>
    /// 选择工具类
    /// </summary>
    public class SelectTools
    {
        private static Document _doc = Application.DocumentManager.MdiActiveDocument;
        private static Database _db = _doc.Database;
        private static Editor _ed = _doc.Editor;

        //选择符合过滤器的所有圆，并返回选择集的ObjectId集合。
        public static ObjectId[] SelectCircles()
        {
            ObjectId[] ids = null;
            //定义选择集过滤器
            TypedValue[] tvs = new TypedValue[]
            {
                new TypedValue((int)DxfCode.Start,"CIRCLE"),
                new TypedValue((int)DxfCode.LayerName,"GCD")
            };
            SelectionFilter sf = new SelectionFilter(tvs);

            PromptStringOptions pso = new PromptStringOptions("选择部分圆<DEFAULT>或[全选<ALL>/帮助<HELP>]")
            {
                UseDefaultValue = true,
                DefaultValue = "DEFAULT",
            };

            PromptResult pr = _ed.GetString(pso);
            PromptSelectionResult psr = null;
            bool continueDo = true;

            while (continueDo)
            {
                if (pr.Status == PromptStatus.OK)
                {
                    switch (pr.StringResult.ToLower())
                    {
                        case "default":
                            psr = _ed.GetSelection(sf);
                            continueDo = false;
                            break;
                        case "all":
                            psr = _ed.SelectAll(sf);
                            continueDo = false;
                            break;
                        case "help":
                            Application.ShowAlertDialog("本插件功能为：\n选择“GCD”图层中的部分（直接选取）或全部（ALL）圆，生成地形曲面。" +
                                "\n开发时间：2018年11月30日。" +
                                "\n修订时间：2019年5月14日。" +
                                "\n作者：乱麻。");
                            break;
                    }
                }
                else if (pr.Status == PromptStatus.Cancel)
                {
                    return ids;
                }
            }

            if (psr.Status == PromptStatus.OK)
            {
                SelectionSet ss = psr.Value;
                ids = ss.GetObjectIds();
                _ed.WriteMessage($"总共选择了{ss.Count.ToString()}个圆！");
            }
            else
            {
                _ed.WriteMessage("没有选择任何对象！");
            }
            return ids;
        }

        //选择符合过滤器的所有块，并返回选择集的Ids集合。
        public ObjectId[] SelectBlocks()
        {
            ObjectId[] ids = null;
            //定义选择集过滤器
            TypedValue[] tvs = new TypedValue[]
            {
                new TypedValue((int)DxfCode.Start,"INSERT")
            };
            SelectionFilter sf = new SelectionFilter(tvs);
            //选择块
            PromptSelectionResult psr = _ed.GetSelection(sf);
            if (psr.Status == PromptStatus.OK)
            {
                SelectionSet ss = psr.Value;
                ids = ss.GetObjectIds();
                _ed.WriteMessage($"总共选择了{ss.Count.ToString()}个块！");
            }
            else
            {
                Application.ShowAlertDialog("没有选择任何对象！");
            }
            return ids;
        }

        //选择符合过滤器的所有单行文字，并返回选择集的Ids集合。
        public ObjectId[] SelectDBTexts()
        {
            ObjectId[] ids = null;
            //定义选择集过滤器
            TypedValue[] tvs = new TypedValue[]
            {
                new TypedValue((int)DxfCode.Start,"TEXT"),
                new TypedValue((int)DxfCode.LayerName,"GCD")
            };
            SelectionFilter sf = new SelectionFilter(tvs);
            //选择文字
            PromptSelectionResult psr = _ed.GetSelection(sf);
            if (psr.Status == PromptStatus.OK)
            {
                SelectionSet ss = psr.Value;
                ids = ss.GetObjectIds();
                _ed.WriteMessage($"总共选择了{ss.Count.ToString()}个文字！");
            }
            else
            {
                Application.ShowAlertDialog("没有选择任何对象！");
            }
            return ids;
        }


        //[CommandMethod("prs")]
        //public static void TestPrompts()
        //{
        //    Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

        //    PromptStringOptions pso = new PromptStringOptions("\nEnter a string or choose from list [Alpha/Bravo/Charlie/Delta]: ");
        //    pso.DefaultValue = "Alpha";
        //    pso.UseDefaultValue = true;
        //    pso.AppendKeywordsToMessage = true;
        //    PromptResult pr = ed.GetString(pso);
        //    ed.WriteMessage("\nChoosen: {0}", pr.StringResult);

        //    PromptKeywordOptions pko = new PromptKeywordOptions("\nEnter a string or choose from list [Alpha/Bravo/Charlie/Delta]: ",
        //        "Alpha Bravo Charlie Delta");
        //    pko.Keywords.Default = "Alpha";
        //    PromptResult res = ed.GetKeywords(pko);
        //    ed.WriteMessage("\nChoosen: {0}", res.StringResult);
        //}
    }
}
